package com.crm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.crm.common.ResVo;
import com.crm.entity.Users;
import com.crm.service.UsersService;
import com.spring4all.swagger.EnableSwagger2Doc;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@EnableSwagger2Doc
@RequestMapping("/users")
@Api(tags = "用户管理")
public class UsersController {

	@Autowired
	private UsersService usersService;

	@GetMapping("/getUsersByPage/{pageIndex}")
	@ApiOperation("获取用户列表带分页")
	public ResVo<Object> getUsersByPage(@RequestParam(value = "name", required = false) @ApiParam("用户名称") String name,
			@PathVariable("pageIndex") @ApiParam("页码") Integer pageIndex) {
		Page<Users> message = usersService.getUsersByPage(pageIndex, name);
		return ResVo.success(message);
	}

	@PostMapping("/addUsers")
	@ApiOperation("添加用户")
	public ResVo<Object> addUsers(@RequestBody Users users) {
		usersService.addUsers(users);
		return ResVo.defaultSuccess();
	}

	@GetMapping("/deleteUsers/{id}")
	@ApiOperation("删除用户")
	public ResVo<Object> deleteUsers(@PathVariable("id") @ApiParam("用户id") String id) {
		usersService.deleteUsers(id);
		return ResVo.defaultSuccess();
	}

	@PostMapping("/login")
	@ApiOperation("用户登录")
	public ResVo<Object> login(@RequestParam("username") @ApiParam("用户名称") String username,
			@RequestParam("password") @ApiParam("用户密码") String password) {
		Users users = usersService.login(username, password);
		if (users != null) {
			return ResVo.success(users);
		}
		return ResVo.defaultFail();
	}

}
